# BuyWins controller controls purchasing of free wins
# In 13wins, the user may purchase free 'wins' (or only a 13 winstreak required for the prize) using their WinPoints 

class BuyWinsController < ApplicationController
  # GET /buy_wins
  # GET /buy_wins.xml
  def index
    if count = params[:sent_count]
      flash[:notice] = "#{} invitation(s) sent. You earn #{count * 50} WinPoints!"
    end
    @buy_wins = BuyWin.find(:all)
    @level = current_user.current_winstreak_count.round + 1
    @installed_friends=facebook_session.fql_query("SELECT uid FROM user WHERE is_app_user = 1 AND uid IN (SELECT uid2 FROM friend WHERE uid1 = #{current_user.id})").join(",")

    respond_to do |format|
      format.fbml # index.fbml.erb
      format.xml  { render :xml => @buy_wins }
    end
  end

  # GET /buy_wins/1
  # GET /buy_wins/1.xml
  def show
    @buy_win = BuyWin.find(params[:id])

    respond_to do |format|
      format.fbml # show.fbml.erb
      format.xml  { render :xml => @buy_win }
    end
  end
  def srpoint
     render :text=>0000,:layout=>false 
  end

  def only
      #current_user.add_point(-1) 
     if current_user.point >= PointConfig.find_by_name("13win_fee").point
      current_user.add_point(-PointConfig.find_by_name("13win_fee").point) 
      current_user.winstreak.streak_count.to_f + 2.to_f
      current_user.winstreak.save
      BuyWin.create(:user_id=>current_user.id,:point_config_id=>0,:winstreak_id=>current_user.winstreak.id)
      flash[:notice] = 'Your next streak will only require 13wins.'
     else
      flash[:error] = 'Not enough WinPoints to make purchase.'
     end
      redirect_to(buy_wins_path) 
  end
  def free
      
      pc = PointConfig.find_by_name_and_level("free",params[:level].to_i)
     if  current_user.point >= pc.point
      BuyWin.create(:user_id=>current_user.id,:point_config_id=>pc.id,:winstreak_id=>current_user.winstreak.id)
      current_user.winstreak.streak_count = current_user.winstreak.streak_count.to_f + 1.to_f
      current_user.winstreak.save
      current_user.add_point(-pc.point) 
      current_user.calc_percentage
      flash[:notice] = 'You have purchased a WIN for your current streak.'
     else
      flash[:error] = 'Not enough WinPoints to make purchase.'
     end
      redirect_to(buy_wins_path) 
  end
  # GET /buy_wins/new
  # GET /buy_wins/new.xml
  def new
    @buy_win = BuyWin.new

    respond_to do |format|
      format.fbml # new.fbml.erb
      format.xml  { render :xml => @buy_win }
    end
  end

  # GET /buy_wins/1/edit
  def edit
    @buy_win = BuyWin.find(params[:id])
  end

  # POST /buy_wins
  # POST /buy_wins.xml
  def create
    @buy_win = BuyWin.new(params[:buy_win])

    respond_to do |format|
      if @buy_win.save
        flash[:notice] = 'BuyWin was successfully created.'
        format.fbml { redirect_to(@buy_win) }
        format.xml  { render :xml => @buy_win, :status => :created, :location => @buy_win }
      else
        format.fbml { render :action => "new" }
        format.xml  { render :xml => @buy_win.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /buy_wins/1
  # PUT /buy_wins/1.xml
  def update
    @buy_win = BuyWin.find(params[:id])

    respond_to do |format|
      if @buy_win.update_attributes(params[:buy_win])
        flash[:notice] = 'BuyWin was successfully updated.'
        format.fbml { redirect_to(@buy_win) }
        format.xml  { head :ok }
      else
        format.fbml { render :action => "edit" }
        format.xml  { render :xml => @buy_win.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /buy_wins/1
  # DELETE /buy_wins/1.xml
  def destroy
    @buy_win = BuyWin.find(params[:id])
    @buy_win.destroy

    respond_to do |format|
      format.fbml { redirect_to(buy_wins_url) }
      format.xml  { head :ok }
    end
  end
end
